Crate normpath

source ·
Expand description

This crate provides methods to normalize paths in the recommended way for the operating system.

It was made to fix a recurring bug caused by using fs::canonicalize on Windows: #45067, #48249, #52440, #55812, #58613, #59107, #74327. Normalization is usually a better choice unless you specifically need a canonical path.

Using these replacement methods will usually fix those issues, but see their documentation for more information:

Additionally, these methods can be used for other enhancements:

§Features

These features are optional and can be enabled or disabled in a “Cargo.toml” file.

§Optional Features

§Implementation

Some methods return Cow to account for platform differences. However, no guarantee is made that the same variant of that enum will always be returned for the same platform. Whichever can be constructed most efficiently will be returned.

All traits are sealed, meaning that they can only be implemented by this crate. Otherwise, backward compatibility would be more difficult to maintain for new features.

§Sponsorship

If this crate has been useful for your project, let me know with a sponsorship! Sponsorships help me create and maintain my open source libraries, and they are always very appreciated.

§Examples

use std::io;
use std::path::Path;

use normpath::BasePathBuf;
use normpath::PathExt;

fn find_target_dir(path: &Path) -> io::Result<Option<BasePathBuf>> {
    let mut path = path.normalize()?;
    while !path.ends_with("target") {
        match path.pop() {
            Ok(true) => continue,
            Ok(false) => {}
            Err(_) => {
                eprintln!("Some components could not be normalized.");
            }
        }
        return Ok(None);
    }
    Ok(Some(path))
}

Modules§

  • The error types defined by this crate.

Structs§

Traits§